package com.cyx.mysql;

import java.sql.*;

public class Demo {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        String url = "jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
        String username = "root";
        String password="123456";
        //1、通过反射加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2、连接数据库
        Connection connection = DriverManager.getConnection(url, username, password);
        //3、执行语句
        Statement statement = connection.createStatement();


//        ResultSet rs = statement.executeQuery("SELECT * from smbms_user");
//        while (rs.next()){
//            System.out.println("id="+rs.getObject("id"));
//            System.out.println("userCode="+rs.getObject("userCode"));
//            System.out.println("userName="+rs.getObject("userName"));
//            System.out.println("birthday="+rs.getObject("birthday"));
//        }
//        rs.close();

        //---------------------------------------------------------
        int ret = statement.executeUpdate("insert into user(userName,birthday,address)values(\"caoyx\",\"2020-10-20\",\"税友大厦\")");
        System.out.println(ret);
        ret = statement.executeUpdate("delete from user where userName=\"caoyx\"");
        System.out.println(ret);

        //预编译
        String sql = "insert into user(userName,birthday,address)values(?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,"jack");
        preparedStatement.setString(2,"2020-10-02");
        preparedStatement.setString(3,"税友大厦");
        ret = preparedStatement.executeUpdate();
        System.out.println("预编译,insert "+ret);

        preparedStatement.close();
        statement.close();
        connection.close();

    }
}
